function [t,Var] = ReadOneVar(Format,FileName,VarName)
% [t,varvarout] = ReadOneVar(Format,FileName,VarName)
% Reads one variables from detailed record.
%
% INPUTS:
%  - Format: 'mpc'|'kmc'|'gas phase'
%  - FileName: the file name of detailed record
%  - VarName: depends on format
% OUTPUT:
% 
% ---------------------------------------------------------
% Format = 'mpc' 
% Column            Variable
% 1                 Run#   
% 2                 t(s)
% 3-7               W[1]~w[5]
% 8                 cost
% 9                 ht
% 10                r2
% 11                g_dens
% 12-31             alpha[0]~[19]
% 32-51             beta[0]~[19]
% 52-56             uplim[0]~[4]
% 57-61             lolim[0]~[4]
% ---------------------------------------------------------
% Format = 'kmc' 
% Column            Variable
% 1                 Run#
% 2                 t(s)
% 3                 T(K)
% 4                 W (Layer/s)
% 5                 rho_s
% 6                 rho_g
% 7                 h
% 8                 r2
% 9-28              alphas[0]-[19]
% 29-48             betas[0]-[19]
% ---------------------------------------------------------
% Format = 'gas phase'
% Column            Variable
% ---------------------------------------------------------

if(strcmp(Format,'mpc'))
    HeaderLineNum = 14;
    ColumnNum  = 61;
    ValidVar = {'RunID',...
                't',...
                'W1','W2','W3','W4','W5',...
                'cost',...
                'ht',...
                'r2',...
                'g_dens',...
                'alpha1','alpha2','alpha3','alpha4','alpha5',...
                'alpha6','alpha7','alpha8','alpha9','alpha10',...
                'alpha11','alpha12','alpha13','alpha14','alpha15',...
                'alpha16','alpha17','alpha18','alpha19','alpha20',...
                'beta1','beta2','beta3','beta4','beta5',...
                'beta6','beta7','beta8','beta9','beta10',...
                'beta11','beta12','beta13','beta14','beta15',...
                'beta16','beta17','beta18','beta19','beta20',...
                'uplim1','uplim2','uplim3','uplim4','uplim5',...
                'lolim1','lolim2','lolim3','lolim4','lolim5'};
elseif(strcmp(Format,'kmc'))
    HeaderLineNum = 13;
    ColumnNum = 48;
    ValidVar = {'RunID',...
                't',...
                'T',...
                'W',...
                'rho_s',...
                'rho_g',...
                'h',...
                'r2',...
                'alpha1','alpha2','alpha3','alpha4','alpha5',...
                'alpha6','alpha7','alpha8','alpha9','alpha10',...
                'alpha11','alpha12','alpha13','alpha14','alpha15',...
                'alpha16','alpha17','alpha18','alpha19','alpha20',...
                'beta1','beta2','beta3','beta4','beta5',...
                'beta6','beta7','beta8','beta9','beta10',...
                'beta11','beta12','beta13','beta14','beta15',...
                'beta16','beta17','beta18','beta19','beta20'};
elseif(strcmp(Format,'gasphase'))
    t = [];
    Var = [];
    return;
end

assert((any(strcmp(VarName,ValidVar))),'Invalid input VARNAME');

index = find(strcmp(VarName,ValidVar),1);
if (index == 1 || index == 2)
    error('Please choose another variable\n');
end

FormatString = '%f %f';
for j = 3:ColumnNum
    if (j == index)
        FormatString = [FormatString,' %f'];
    else
        FormatString = [FormatString,' %*f'];
    end
end
fid = fopen(FileName);
data = textscan(fid,FormatString,'HeaderLines',HeaderLineNum,'TreatAsEmpty',{'-1.#IND','1.#INF'},'EmptyValue',NaN);
fclose(fid);

IDs = data{1};
TotalRecordNum = length(IDs);
RecordSetsNum = max(IDs);
RecordNumPerSet = TotalRecordNum/RecordSetsNum;
time = data{2};
AllVar = data{3};
Var = zeros(RecordNumPerSet,RecordSetsNum);
for j = 1:RecordSetsNum   
    Var(:,j) = AllVar(((j-1)*RecordNumPerSet+1):j*RecordNumPerSet);
end
t = time(1:RecordNumPerSet);
